package com.hs.server.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hs.server.domain.ShResourcexx;
import com.hs.server.domain.ShResourcexxRecord;
import com.hs.server.mapper.ShResourcexxRecordMapper;
import com.hs.server.service.ShResourcexxRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ShResourcexxRecordServiceImpl implements ShResourcexxRecordService{
    @Autowired
    private ShResourcexxRecordMapper shResourcexxRecordMapper;
    private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
    @Override
    public synchronized void shzcsIncrease(ShResourcexx shResourcexx) {
        ShResourcexxRecord shResourcexxRecord = new ShResourcexxRecord();
        String ipdz = shResourcexx.getIpdz();
        //获取上次该服务器的审核日期
        String lastDate = shResourcexxRecordMapper.getLastDate(ipdz);
        if (StringUtils.isBlank(lastDate) || !lastDate.equals(df.format(new Date()))){
            shResourcexxRecord.setIpdz(ipdz);
            shResourcexxRecord.setDkh(shResourcexx.getDkh());
            shResourcexxRecord.setShrq(new Date());
            shResourcexxRecord.setClcs("0");
            shResourcexxRecord.setShzcs("1");
            shResourcexxRecordMapper.insert(shResourcexxRecord);
        } else {//同一天
            //获取该ipdz的当前日期审核记录
            shResourcexxRecord = shResourcexxRecordMapper.getShResourcexxRecord(ipdz,lastDate);
            int shzcs = Integer.parseInt(shResourcexxRecord.getShzcs());
            shzcs++;
            shResourcexxRecord.setShzcs(String.valueOf(shzcs));
            QueryWrapper<ShResourcexxRecord> qw = new QueryWrapper<>();
            qw.eq("shrq",lastDate);
            qw.eq("ipdz",ipdz);
            shResourcexxRecordMapper.update(shResourcexxRecord,qw);
        }

    }

    @Override
    public synchronized void clcsIncrease(ShResourcexx shResourcexx) {
        ShResourcexxRecord shResourcexxRecord = new ShResourcexxRecord();
        String ipdz = shResourcexx.getIpdz();
        //获取上次该服务器的审核日期
        String lastDate = shResourcexxRecordMapper.getLastDate(ipdz);
        if (StringUtils.isBlank(lastDate) || !lastDate.equals(df.format(new Date()))){
            shResourcexxRecord.setIpdz(ipdz);
            shResourcexxRecord.setDkh(shResourcexx.getDkh());
            shResourcexxRecord.setShrq(new Date());
            shResourcexxRecord.setClcs("1");
            shResourcexxRecord.setShzcs("0");
            shResourcexxRecordMapper.insert(shResourcexxRecord);
        }else {//同一天
            //获取该ipdz的当前日期审核记录
            shResourcexxRecord = shResourcexxRecordMapper.getShResourcexxRecord(ipdz,lastDate);
            int clcs = Integer.parseInt(shResourcexxRecord.getClcs());
            clcs++;
            shResourcexxRecord.setClcs(String.valueOf(clcs));
            QueryWrapper<ShResourcexxRecord> qw = new QueryWrapper<>();
            qw.eq("shrq",lastDate);
            qw.eq("ipdz",ipdz);
            shResourcexxRecordMapper.update(shResourcexxRecord,qw);
        }
    }
}
